home *** CD-ROM | disk | FTP | other *** search
/ X User Tools / X User Tools (O'Reilly and Associates)(1994).ISO / sun4c / archive / tcltk.z / tcltk / man / cat3 / TraceVar.3 < prev    next >
Text File  |  1994-09-20  |  20KB  |  463 lines

  1.  
  2.  
  3.  
  4. Tcl_TraceVar(3)      Tcl Library Procedures
  5.  
  6.  
  7.  
  8. _________________________________________________________________
  9.  
  10. NAME
  11.      Tcl_TraceVar,         Tcl_TraceVar2,         Tcl_UntraceVar,
  12.      Tcl_UntraceVar2, Tcl_VarTraceInfo, Tcl_VarTraceInfo2 - moni-
  13.      tor accesses to a variable
  14.  
  15. SYNOPSIS
  16.      #include <tcl.h>
  17.  
  18.      int
  19.      Tcl_TraceVar(_i_n_t_e_r_p, _v_a_r_N_a_m_e, _f_l_a_g_s, _p_r_o_c, _c_l_i_e_n_t_D_a_t_a)
  20.  
  21.      int
  22.      Tcl_TraceVar2(_i_n_t_e_r_p, _n_a_m_e_1, _n_a_m_e_2, _f_l_a_g_s, _p_r_o_c, _c_l_i_e_n_t_D_a_t_a)
  23.  
  24.      Tcl_UntraceVar(_i_n_t_e_r_p, _v_a_r_N_a_m_e, _f_l_a_g_s, _p_r_o_c, _c_l_i_e_n_t_D_a_t_a)
  25.  
  26.      Tcl_UntraceVar2(_i_n_t_e_r_p, _n_a_m_e_1, _n_a_m_e_2, _f_l_a_g_s, _p_r_o_c, _c_l_i_e_n_t_D_a_t_a)
  27.  
  28.      ClientData
  29.      Tcl_VarTraceInfo(_i_n_t_e_r_p, _v_a_r_N_a_m_e, _f_l_a_g_s, _p_r_o_c, _p_r_e_v_C_l_i_e_n_t_D_a_t_a)
  30.  
  31.      ClientData
  32.      Tcl_VarTraceInfo2(_i_n_t_e_r_p, _n_a_m_e_1, _n_a_m_e_2, _f_l_a_g_s, _p_r_o_c, _p_r_e_v_C_l_i_e_n_t_D_a_t_a)
  33.  
  34. ARGUMENTS
  35.      Tcl_Interp         *_i_n_t_e_r_p          (in)      Interpreter
  36.                                                    containing
  37.                                                    variable.
  38.  
  39.      char               *_v_a_r_N_a_m_e         (in)      Name of  vari-
  40.                                                    able.      May
  41.                                                    refer   to   a
  42.                                                    scalar   vari-
  43.                                                    able,  to   an
  44.                                                    array variable
  45.                                                    with no index,
  46.                                                    or to an array
  47.                                                    variable  with
  48.                                                    a
  49.                                                    parenthesized
  50.                                                    index.
  51.  
  52.      int                _f_l_a_g_s            (in)      OR-ed combina-
  53.                                                    tion   of  the
  54.                                                    values
  55.                                                    TCL_TRACE_READS,
  56.                                                    TCL_TRACE_WRITES,
  57.                                                    and
  58.                                                    TCL_TRACE_UNSETS,
  59.                                                    and
  60.  
  61.  
  62.  
  63. Tcl                                                             1
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70. Tcl_TraceVar(3)      Tcl Library Procedures
  71.  
  72.  
  73.  
  74.                                                    TCL_GLOBAL_ONLY.
  75.                                                    Not  all flags
  76.                                                    are  used   by
  77.                                                    all       pro-
  78.                                                    cedures.   See
  79.                                                    below for more
  80.                                                    information.
  81.  
  82.      Tcl_VarTraceProc   *_p_r_o_c            (in)      Procedure   to
  83.                                                    invoke   when-
  84.                                                    ever  one   of
  85.                                                    the     traced
  86.                                                    operations
  87.                                                    occurs.
  88.  
  89.      ClientData         _c_l_i_e_n_t_D_a_t_a       (in)      Arbitrary
  90.                                                    one-word value
  91.                                                    to   pass   to
  92.                                                    _p_r_o_c.
  93.  
  94.      char               *_n_a_m_e_1           (in)      Name of scalar
  95.                                                    or array vari-
  96.                                                    able  (without
  97.                                                    array index).
  98.  
  99.      char               *_n_a_m_e_2           (in)      For a trace on
  100.                                                    an  element of
  101.                                                    an      array,
  102.                                                    gives      the
  103.                                                    index  of  the
  104.                                                    element.   For
  105.                                                    traces      on
  106.                                                    scalar   vari-
  107.                                                    ables  or   on
  108.                                                    whole  arrays,
  109.                                                    is NULL.
  110.  
  111.      ClientData         _p_r_e_v_C_l_i_e_n_t_D_a_t_a   (in)      If   non-NULL,
  112.                                                    gives     last
  113.                                                    value returned
  114.                                                    by
  115.                                                    Tcl_VarTraceInfo
  116.                                                    or
  117.                                                    Tcl_VarTraceInfo2,
  118.                                                    so  this  call
  119.                                                    will    return
  120.                                                    information
  121.                                                    about     next
  122.                                                    trace.      If
  123.                                                    NULL,     this
  124.                                                    call      will
  125.                                                    return
  126.  
  127.  
  128.  
  129. Tcl                                                             2
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136. Tcl_TraceVar(3)      Tcl Library Procedures
  137.  
  138.  
  139.  
  140.                                                    information
  141.                                                    about    first
  142.                                                    trace.
  143. _________________________________________________________________
  144.  
  145.  
  146. DESCRIPTION
  147.      Tcl_TraceVar allows a C procedure  to  monitor  and  control
  148.      access to a Tcl variable, so that the C procedure is invoked
  149.      whenever the variable is read or written or unset.   If  the
  150.      trace  is  created  successfully  then  Tcl_TraceVar returns
  151.      TCL_OK.  If an error occurred  (e.g.  _v_a_r_N_a_m_e  specifies  an
  152.      element of an array, but the actual variable isn't an array)
  153.      then TCL_ERROR is returned and an error message is  left  in
  154.      _i_n_t_e_r_p->_r_e_s_u_l_t.
  155.  
  156.      The _f_l_a_g_s argument to Tcl_TraceVar indicates when the  trace
  157.      procedure is to be invoked and provides information for set-
  158.      ting up the trace.  It consists of an OR-ed  combination  of
  159.      any of the following values:
  160.  
  161.      TCL_GLOBAL_ONLY
  162.           Normally, the variable will be looked up at the current
  163.           level  of  procedure call;  if this bit is set then the
  164.           variable will be looked up at  global  level,  ignoring
  165.           any active procedures.
  166.  
  167.      TCL_TRACE_READS
  168.           Invoke _p_r_o_c whenever an attempt is  made  to  read  the
  169.           variable.
  170.  
  171.      TCL_TRACE_WRITES
  172.           Invoke _p_r_o_c whenever an attempt is made to  modify  the
  173.           variable.
  174.  
  175.      TCL_TRACE_UNSETS
  176.           Invoke _p_r_o_c whenever the variable is unset.  A variable
  177.           may  be unset either explicitly by an unset command, or
  178.           implicitly when a procedure returns  (its  local  vari-
  179.           ables  are automatically unset) or when the interpreter
  180.           is deleted (all variables are automatically unset).
  181.  
  182.      Whenever one of the specified operations occurs on the vari-
  183.      able,  _p_r_o_c  will  be invoked.  It should have arguments and
  184.      result that match the type Tcl_VarTraceProc:
  185.           typedef char *Tcl_VarTraceProc(
  186.                ClientData _c_l_i_e_n_t_D_a_t_a,
  187.                Tcl_Interp *_i_n_t_e_r_p,
  188.                char *_n_a_m_e_1,
  189.                char *_n_a_m_e_2,
  190.                int _f_l_a_g_s);
  191.      The _c_l_i_e_n_t_D_a_t_a and _i_n_t_e_r_p  parameters  will  have  the  same
  192.  
  193.  
  194.  
  195. Tcl                                                             3
  196.  
  197.  
  198.  
  199.  
  200.  
  201.  
  202. Tcl_TraceVar(3)      Tcl Library Procedures
  203.  
  204.  
  205.  
  206.      values  as  those  passed to Tcl_TraceVar when the trace was
  207.      created.  _C_l_i_e_n_t_D_a_t_a typically  points  to  an  application-
  208.      specific  data structure that describes what to do when _p_r_o_c
  209.      is invoked.  _N_a_m_e_1 and _n_a_m_e_2 give the  name  of  the  traced
  210.      variable in the normal two-part form (see the description of
  211.      Tcl_TraceVar2 below for details).  _F_l_a_g_s is an OR-ed  combi-
  212.      nation of bits providing several pieces of information.  One
  213.      of   the   bits   TCL_TRACE_READS,   TCL_TRACE_WRITES,    or
  214.      TCL_TRACE_UNSETS  will  be  set  in  _f_l_a_g_s to indicate which
  215.      operation is being  performed  on  the  variable.   The  bit
  216.      TCL_GLOBAL_ONLY  will  be  set  whenever  the variable being
  217.      accessed is a global one not  accessible  from  the  current
  218.      level  of  procedure call:  the trace procedure will need to
  219.      pass this flag  back  to  variable-related  procedures  like
  220.      Tcl_GetVar  if  it attempts to access the variable.  The bit
  221.      TCL_TRACE_DESTROYED will be set in _f_l_a_g_s  if  the  trace  is
  222.      about  to  be  destroyed;  this information may be useful to
  223.      _p_r_o_c so that it can clean up its own  internal  data  struc-
  224.      tures  (see  the  section TCL_TRACE_DESTROYED below for more
  225.      details).  Lastly, the bit TCL_INTERP_DESTROYED will be  set
  226.      if the entire interpreter is being destroyed.  When this bit
  227.      is set, _p_r_o_c must be especially careful  in  the  things  it
  228.      does  (see  the  section  TCL_INTERP_DESTROYED  below).  The
  229.      trace procedure's return value should normally be NULL;  see
  230.      ERROR RETURNS below for information on other possibilities.
  231.  
  232.      Tcl_UntraceVar may be used to remove a trace.  If the  vari-
  233.      able specified by _i_n_t_e_r_p, _v_a_r_N_a_m_e, and _f_l_a_g_s has a trace set
  234.      with _f_l_a_g_s, _p_r_o_c, and  _c_l_i_e_n_t_D_a_t_a,  then  the  corresponding
  235.      trace is removed.  If no such trace exists, then the call to
  236.      Tcl_UntraceVar has no effect.  The same bits are  valid  for
  237.      _f_l_a_g_s as for calls to Tcl_TraceVars.
  238.  
  239.      Tcl_VarTraceInfo may be used to retrieve  information  about
  240.      traces  set  on  a  given  variable.   The return value from
  241.      Tcl_VarTraceInfo is the _c_l_i_e_n_t_D_a_t_a associated with a partic-
  242.      ular  trace.  The trace must be on the variable specified by
  243.      the  _i_n_t_e_r_p,  _v_a_r_N_a_m_e,  and  _f_l_a_g_s   arguments   (only   the
  244.      TCL_GLOBAL_ONLY  bit  from  _f_l_a_g_s  is  used;  other bits are
  245.      ignored) and its trace procedure must the same as  the  _p_r_o_c
  246.      argument.   If  the _p_r_e_v_C_l_i_e_n_t_D_a_t_a argument is NULL then the
  247.      return  value  corresponds  to  the  first  (most   recently
  248.      created)  matching  trace,  or NULL if there are no matching
  249.      traces.  If the _p_r_e_v_C_l_i_e_n_t_D_a_t_a argument isn't NULL, then  it
  250.      should   be  the  return  value  from  a  previous  call  to
  251.      Tcl_VarTraceInfo.  In this case, the new return  value  will
  252.      correspond  to  the  next matching trace after the one whose
  253.      _c_l_i_e_n_t_D_a_t_a matches  _p_r_e_v_C_l_i_e_n_t_D_a_t_a,  or  NULL  if  no  trace
  254.      matches  _p_r_e_v_C_l_i_e_n_t_D_a_t_a  or  if  there  are no more matching
  255.      traces after it.  This mechanism makes it possible  to  step
  256.      through all of the traces for a given variable that have the
  257.      same _p_r_o_c.
  258.  
  259.  
  260.  
  261. Tcl                                                             4
  262.  
  263.  
  264.  
  265.  
  266.  
  267.  
  268. Tcl_TraceVar(3)      Tcl Library Procedures
  269.  
  270.  
  271.  
  272. TWO-PART NAMES
  273.      The   procedures   Tcl_TraceVar2,    Tcl_UntraceVar2,    and
  274.      Tcl_VarTraceInfo2    are    identical    to    Tcl_TraceVar,
  275.      Tcl_UntraceVar, and Tcl_VarTraceInfo,  respectively,  except
  276.      that  the name of the variable has already been separated by
  277.      the caller into two parts.  _N_a_m_e_1 gives the name of a scalar
  278.      variable  or  array,  and _n_a_m_e_2 gives the name of an element
  279.      within an array.  If _n_a_m_e_2 is NULL it means that either  the
  280.      variable is a scalar or the trace is to be set on the entire
  281.      array rather than an  individual  element  (see  WHOLE-ARRAY
  282.      TRACES below for more information).
  283.  
  284.  
  285. ACCESSING VARIABLES DURING TRACES
  286.      During read and write traces, the trace procedure can  read,
  287.      write,  or  unset  the  traced  variable  using Tcl_GetVar2,
  288.      Tcl_SetVar2, and other procedures.  While _p_r_o_c is executing,
  289.      traces  are  temporarily  disabled for the variable, so that
  290.      calls to Tcl_GetVar2 and Tcl_SetVar2 will not cause _p_r_o_c  or
  291.      other  trace procedures to be invoked again.  Disabling only
  292.      occurs for the variable whose  trace  procedure  is  active;
  293.      accesses  to other variables will still be traced.  However,  |
  294.      if a variable is unset during a read  or  write  trace  then  |
  295.      unset traces will be invoked.
  296.  
  297.      During unset traces the variable has already been completely
  298.      expunged.  It is possible for the trace procedure to read or
  299.      write the variable, but this will be a new  version  of  the
  300.      variable.   Traces  are  not disabled during unset traces as
  301.      they are for read and write traces, but existing traces have
  302.      been  removed  from the variable before any trace procedures
  303.      are invoked.  If new traces are  set  by  unset  trace  pro-
  304.      cedures,  these  traces  will  be invoked on accesses to the
  305.      variable by the trace procedures.
  306.  
  307.  
  308. CALLBACK TIMING
  309.      When read tracing has been specified  for  a  variable,  the
  310.      trace  procedure  will  be  invoked  whenever the variable's
  311.      value is read.  This includes set Tcl  commands,  $-notation
  312.      in  Tcl  commands,  and  invocations  of  the Tcl_GetVar and
  313.      Tcl_GetVar2 procedures.  _P_r_o_c is  invoked  just  before  the
  314.      variable's  value  is  returned.  It may modify the value of
  315.      the variable to  affect  what  is  returned  by  the  traced
  316.      access.   If  it  unsets  the  variable then the access will  |
  317.      return an error just as if the variable never existed.
  318.  
  319.      When write tracing has been specified for  a  variable,  the
  320.      trace  procedure  will  be  invoked  whenever the variable's
  321.      value is modified.  This  includes  set  commands,  commands
  322.      that  modify  variables  as  side effects (such as catch and
  323.      scan),  and  calls  to  the   Tcl_SetVar   and   Tcl_SetVar2
  324.  
  325.  
  326.  
  327. Tcl                                                             5
  328.  
  329.  
  330.  
  331.  
  332.  
  333.  
  334. Tcl_TraceVar(3)      Tcl Library Procedures
  335.  
  336.  
  337.  
  338.      procedures).   _P_r_o_c  will  be  invoked  after the variable's
  339.      value has been modified, but before the  new  value  of  the
  340.      variable  has been returned.  It may modify the value of the
  341.      variable to override the change and to determine  the  value
  342.      actually  returned  by the traced access.  If it deletes the  |
  343.      variable then the traced access will return an empty string.
  344.  
  345.      When unset tracing has been specified, the  trace  procedure
  346.      will  be  invoked  whenever  the variable is destroyed.  The
  347.      traces will be called after the variable has been completely
  348.      unset.
  349.  
  350.  
  351. WHOLE-ARRAY TRACES
  352.      If a call to Tcl_TraceVar  or  Tcl_TraceVar2  specifies  the
  353.      name  of  an array variable without an index into the array,
  354.      then the trace will be set on the array as  a  whole.   This
  355.      means  that _p_r_o_c will be invoked whenever any element of the
  356.      array is accessed in the ways specified by _f_l_a_g_s.   When  an
  357.      array  is  unset,  a  whole-array trace will be invoked just
  358.      once, with _n_a_m_e_1 equal to the name of the  array  and  _n_a_m_e_2
  359.      NULL;  it will not be invoked once for each element.
  360.  
  361.  
  362. MULTIPLE TRACES
  363.      It is possible for multiple traces  to  exist  on  the  same
  364.      variable.   When  this  happens, all of the trace procedures
  365.      will  be  invoked  on  each  access,  in  order  from  most-
  366.      recently-created   to  least-recently-created.   When  there
  367.      exist whole-array traces for an array as well as  traces  on
  368.      individual  elements,  the  whole-array  traces  are invoked
  369.      before the individual-element traces.  If a  read  or  write  |
  370.      trace  unsets the variable then all of the unset traces will  |
  371.      be invoked but the remainder of the read  and  write  traces  |
  372.      will be skipped.
  373.  
  374.  
  375. ERROR RETURNS
  376.      Under normal conditions trace procedures should return NULL,
  377.      indicating  successful  completion.   If _p_r_o_c returns a non-
  378.      NULL value it signifies that an error occurred.  The  return
  379.      value  must  be  a pointer to a static character string con-
  380.      taining an error message.  If a trace procedure  returns  an
  381.      error,  no further traces are invoked for the access and the
  382.      traced access aborts with the  given  message.   Trace  pro-
  383.      cedures  can  use this facility to make variables read-only,
  384.      for example (but note that the value of  the  variable  will
  385.      already  have  been  modified  before the trace procedure is
  386.      called, so the trace procedure  will  have  to  restore  the
  387.      correct value).
  388.  
  389.  
  390.  
  391.  
  392.  
  393. Tcl                                                             6
  394.  
  395.  
  396.  
  397.  
  398.  
  399.  
  400. Tcl_TraceVar(3)      Tcl Library Procedures
  401.  
  402.  
  403.  
  404.      The return value from _p_r_o_c is  only  used  during  read  and
  405.      write  tracing.   During  unset  traces, the return value is
  406.      ignored and all relevant trace  procedures  will  always  be
  407.      invoked.
  408.  
  409.  
  410. RESTRICTIONS
  411.      A trace procedure can be called at any time, even when there
  412.      is  a  partically-formed  result in the interpreter's result
  413.      area.  If the trace procedure does anything that could  dam-
  414.      age this result (such as calling Tcl_Eval) then it must save
  415.      the original values of the interpreter's result and freeProc
  416.      fields and restore them before it returns.
  417.  
  418.  
  419. UNDEFINED VARIABLES
  420.      It is legal to set a trace on an  undefined  variable.   The
  421.      variable  will  still appear to be undefined until the first
  422.      time its value is set.  If an undefined variable  is  traced
  423.      and then unset, the unset will fail with an error (``no such
  424.      variable''), but the trace procedure will still be invoked.
  425.  
  426.  
  427. TCL_TRACE_DELETED FLAG
  428.      In an unset callback to _p_r_o_c, the TCL_TRACE_DELETED  bit  is
  429.      set  in  _f_l_a_g_s  if the trace is being removed as part of the
  430.      deletion.  Traces on a variable are always removed  whenever
  431.      the  variable  is  deleted;  the only time TCL_TRACE_DELETED
  432.      isn't set is for a whole-array trace  invoked  when  only  a
  433.      single element of an array is unset.
  434.  
  435.  
  436. TCL_INTERP_DESTROYED
  437.      When an interpreter is destroyed, unset  traces  are  called
  438.      for all of its variables.  The TCL_INTERP_DESTROYED bit will
  439.      be set in the _f_l_a_g_s argument passed to the trace procedures.
  440.      Trace  procedures  must be extremely careful in what they do
  441.      if the TCL_INTERP_DESTROYED bit is set.  It is not safe  for
  442.      the  procedures  to  invoke any Tcl procedures on the inter-
  443.      preter, since its state  is  partially  deleted.   All  that
  444.      trace  procedures  should do under these circumstances is to
  445.      clean up and free their own internal data structures.
  446.  
  447.  
  448. BUGS
  449.      Tcl doesn't do any error  checking  to  prevent  trace  pro-
  450.      cedures  from  misusing  the  interpreter during traces with
  451.      TCL_INTERP_DESTROYED set.
  452.  
  453.  
  454. KEYWORDS
  455.      clientData, trace, variable
  456.  
  457.  
  458.  
  459. Tcl                                                             7
  460.  
  461.  
  462.  
  463.